// arch-tag: 5a41f806-08ef-4726-a32b-706b96785cc9
package de.yvert.jingle.tools;

import java.beans.PropertyChangeListener;
import java.util.HashMap;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.KeyStroke;
import javax.swing.event.SwingPropertyChangeSupport;

/**
 * @see Action
 */
public abstract class CustomAction implements Action
{

private boolean enabled = true;
private HashMap<String,Object> hashMap = new HashMap<String,Object>();
private SwingPropertyChangeSupport changeSupport = new SwingPropertyChangeSupport(this);

public CustomAction()
{/*OK*/}

public CustomAction(String name)
{
	putValue(NAME, name);
	putValue(ACTION_COMMAND_KEY, name);
}

public CustomAction(String name, KeyStroke accelerator)
{
	this(name);
	putValue(ACCELERATOR_KEY, accelerator);
}

public CustomAction(String name, Icon icon)
{
	this(name);
	putValue(SMALL_ICON, icon);
}

public String getName()
{ return (String) getValue(NAME); }

public void setAccelerator(KeyStroke keyStroke)
{ putValue(ACCELERATOR_KEY, keyStroke); }

public KeyStroke getAccelerator()
{ return (KeyStroke) getValue(ACCELERATOR_KEY); }

public Object getValue(String key)
{ return hashMap.get(key); }

public void putValue(String key, Object newValue)
{
	Object oldValue = null;
	
	if (hashMap.containsKey(key))
		oldValue = hashMap.get(key);
	
	if (newValue == null)
		hashMap.remove(key);
	else
		hashMap.put(key, newValue);
	
	firePropertyChange(key, oldValue, newValue);
}

public boolean isEnabled()
{ return enabled; }

public void setEnabled(boolean newValue)
{
	boolean oldValue = this.enabled;
	
	if (oldValue != newValue)
	{
		this.enabled = newValue;
		firePropertyChange("enabled", 
		  Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
	}
}

public Object[] getKeys()
{ return hashMap.keySet().toArray(); }

protected void firePropertyChange(String propertyName, Object oldValue, Object newValue)
{
	if (oldValue != null && newValue != null && oldValue.equals(newValue))
		return;
	changeSupport.firePropertyChange(propertyName, oldValue, newValue);
}

public synchronized void addPropertyChangeListener(PropertyChangeListener l)
{ changeSupport.addPropertyChangeListener(l); }

public synchronized void removePropertyChangeListener(PropertyChangeListener l)
{ changeSupport.removePropertyChangeListener(l); }

public synchronized PropertyChangeListener[] getPropertyChangeListeners()
{ return changeSupport.getPropertyChangeListeners(); }

}
